require 'fileutils'

namespace "build" do
    task :all do
        rhorootdir = ENV['RHO_ROOT']
        raise "RHO_ROOT is not set" if rhorootdir.nil?

        $targetdir = ENV['TARGET_TEMP_DIR']
        raise "TARGET_TEMP_DIR is not set" if $targetdir.nil?

        $configuration = ENV['CONFIGURATION']
        raise "CONFIGURATION is not set" if $configuration.nil?

        $sdk = ENV['SDK_NAME']
        raise "SDK_NAME is not set" if $sdk.nil?

        require File.join(rhorootdir, 'platform','iphone','rbuild','buildextension')

        IPhoneBuild.load_env_for_build_rhodes_ios_extension


        simulator = $sdk =~ /iphonesimulator/

        if $configuration == 'Distribution'
           $configuration = 'Release'
        end

        result_dir = "#{$rootdir}/lib/extensions/openssl.so/ext/iphone/" + ( simulator ? "Release-iphonesimulator" : "Release-iphoneos")
        result_lib = result_dir + '/libopenssl.so.a'
        target_lib = $targetdir + '/libopenssl.so.a'
        #if !File.exist?(result_lib)

            extension_name = "openssl.so"
            extension_lib_name = "openssl.so"
            extension_xcode_target_name = "openssl" + ( simulator ? "_sim" : "")

            ret= IPhoneBuild.build_rhodes_ios_extension(extension_name, extension_lib_name, extension_xcode_target_name)

            mkdir_p result_dir

            #make cached bin
            cp target_lib, result_lib

        #else
        #    # copy cached bin to target
        #    cp result_lib,target_lib
        #end


    end
end

task :default => "build:all"
